/*	kd:
	
	Strife is Doom but with a metric ton of keys for some reason. It also has
	pope screens for its MiSsIoN sYsTeM.
	
	*/

class Gc_StrifeSbar : Gc_DoomSbar {
	const pop_fade_tics = 10;
	
	protected FadeTick popup_t;
	protected transient BrokenLines log_text;
	
	// Some sprites...
	protected Gc_SbarMoveSprite		sprite_log;
	protected int					sprite_log_width_2;
	protected int					sprite_log_width;
	protected int					sprite_log_border;
	
	override void Init () {
		super.Init();
		
		sprite_log.Load("datbar");
		sprite_log.StartRelativeToSize(0, 0.2);
		sprite_log.EndRelativeToSize(0, -1.5);
		
		sprite_log_border	= 8;
		sprite_log_width	= sprite_log.Width();
		sprite_log_width_2	= sprite_log_width / 2;
		
		// sprite_log = Gc_Sprite.Load("datbar");
	}
	
	override void Draw (int state, double t) {
		if(NoHud()) {
			DrawPope(t);
			return;
		}
		
		super.Draw(state, t);
		// DrawAllKeysTopRight(0, 0, 0.5, alpha: automap_alpha);
		DrawPope(t);
	}
	
	protected void DrawPope (double t) const {
		if(!IsPopVisible()) {
			return;
		}
		
		switch(current_pop_i) {
		case POP_KEYS:
			DrawPopeOfKeys(t);
			break;
		case POP_STATUS:
			DrawPopeOfStatus(t);
			break;
		case POP_LOG:
			DrawPopeOfClogjes(t);
			break;
		}
	}
	
	protected void DrawPopeOfKeys (double t) const {
		
	}
	
	protected void DrawPopeOfStatus (double t) const {
		
	}
	
	protected void DrawPopeOfClogjes (double t) {
		double	anim_f		=	popup_t.RenderStandardDelta(pop_fade_tics, t);
		vector2	sprite_pos	=	sprite_log.DrawPos(anim_f);
		sprite_pos.x		-=	sprite_log_width_2 - sprite_log_border;
		sprite_pos.y		+=	sprite_log_border;
		
		sprite_log.DrawBottomCenter(self, anim_f, alpha: anim_f * 0.5);
		
		DrawString(
			text_font,
			"Objective",
			sprite_pos,
			DI_ITEM_LEFT_TOP | DI_SCREEN_CENTER_BOTTOM,
			translation: Font.cr_fire,
			alpha: anim_f);
		
		DrawString(
			mono_font,
			level.TimeFormatted(),
			sprite_pos + (sprite_log_width - 2 * sprite_log_border, 0),
			DI_TEXT_ALIGN_RIGHT | DI_ITEM_RIGHT_TOP | DI_SCREEN_CENTER_BOTTOM,
			translation: Font.cr_fire,
			alpha: anim_f);
		
		for(int i = 0; i < log_text.count(); ++i) {
			DrawString(
				text_font,
				log_text.StringAt(i),
				sprite_pos + (0, (i + 1) * text_font.mfont.GetHeight()),
				DI_ITEM_LEFT_TOP | DI_SCREEN_CENTER_BOTTOM,
				translation: Font.cr_ice,
				alpha: anim_f);
		}
	}
	
	override void Tick () {
		super.Tick();
		
		// kd: Handle the popsicles:
		popup_t.Next();
		
		if(!IsPopVisible()) {
			
			if(has_next_pop) {
				popup_t.ContinueTo(pop_fade_tics);
				has_next_pop = false;
				current_pop_i = last_clicked_pop_i;
				
				// Init:
				if(current_pop_i == POP_LOG) {
					String log_lowercase = cplayer.logtext;
					log_lowercase.ToLower();
					
					log_text = text_font.mfont.BreakLines(
						log_lowercase,
						sprite_log_width - 2 * sprite_log_border);
				}
			}
		}
		
		else {}
	}
	
	// kd: This is executed whenever you press any one of the popup keys for
	// weapons, keys or the EpIc MiSsIoN lOg.
	
	// The idea is that Tick contains the code to make any one popup show, and
	// ShowPop is merely the flow control to tell one to disappear or appear.
	// We don't have to store anything for that except use the time of the
	// FadeTick as an indicator and what popsicle to use next.
	
	protected bool	is_pop_shown;
	protected bool	has_next_pop;
	protected int	last_clicked_pop_i;
	protected int	current_pop_i;
	
	override void ShowPop (int pop_i) {
		last_clicked_pop_i = pop_i;
		
		if(pop_i != current_pop_i || !IsPopVisible()) {
			has_next_pop = pop_i != POP_NONE;
			popup_t.ContinueTo(0);
		}
		
		else if(IsPopVisible() && pop_i == current_pop_i) {
			popup_t.ContinueTo(0);
		}
	}
	
	bool IsPopVisible () const {
		return 0 < popup_t.Now();
	}
}